<html>
<head><meta charset="utf-8"><title>Type alias with trait bound warning vs error · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20alias.20with.20trait.20bound.20warning.20vs.20error.html">Type alias with trait bound warning vs error</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="235515143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20alias%20with%20trait%20bound%20warning%20vs%20error/near/235515143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Weaver <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20alias.20with.20trait.20bound.20warning.20vs.20error.html#235515143">(Apr 21 2021 at 14:02)</a>:</h4>
<p>In the following example I can't exclude the trait bound on the type alias or it won't compile, but I get a warning if I include it:</p>
<div class="codehilite"><pre><span></span><code>struct A1 {}
struct B1 {}

struct A2 {}
struct B2 {}

trait CorrespondingB { type B; }
impl CorrespondingB for A1 { type B = B1; }
impl CorrespondingB for A2 { type B = B2; }

// This gives a warning that the constraint is not enforced in the alias:
type MyAlias&lt;X: CorrespondingB&gt; = (X, X::B);
// This gives an error that the associated type doesn&#39;t exist:
// type MyAlias&lt;X&gt; = (X, X::B);

fn f&lt;X: CorrespondingB&gt;(pair: MyAlias&lt;X&gt;) -&gt; X { pair.0 }

fn main() {
    f((A1{}, B1{}));
}
</code></pre></div>
<p>What do I do with this? I expect what I'm doing is weird, but it does smell a little like it would be worth opening an issue...</p>



<a name="235519726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Type%20alias%20with%20trait%20bound%20warning%20vs%20error/near/235519726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Weaver <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Type.20alias.20with.20trait.20bound.20warning.20vs.20error.html#235519726">(Apr 21 2021 at 14:30)</a>:</h4>
<p>The detail in the warning message actually contained the solution to making the error go away. The alias should read:</p>
<div class="codehilite"><pre><span></span><code>type MyAlias&lt;X&gt; = (X, &lt;X as CorrespondingB&gt;::B);
</code></pre></div>
<p>Thanks <span aria-label="duck" class="emoji emoji-1f986" role="img" title="duck">:duck:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>